package zsd.reiview.utility;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;


import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

public class StringUtility extends Utility {

	/**
	 * @Title: subString
	 * @Description: 第一个start end之间的字符
	 * @param @param str
	 * @param @param start
	 * @param @param end
	 * @param @return
	 * @return String
	 * @throws
	 * @date 2011-8-15 下午08:26:49
	 */

	public static String subString(String str, String start, String end) {
		String sub = null;
		int m, n;
		if (start != end && (m = str.indexOf(start)) != -1 && (n = str.indexOf(end)) != -1)
			sub = str.substring(m + start.length(), n);
		else if (start == end && (m = str.indexOf(start)) != -1 && (n = str.indexOf(end, m + start.length())) != -1) {
			sub = str.substring(m + start.length(), n);
		}
		return sub;
	}

	/**
	 * @Title: leftString
	 * @Description:去掉第一个start end之间的字符 剩下的
	 * @param @param str
	 * @param @param start
	 * @param @param end
	 * @param @return
	 * @return String
	 * @throws
	 * @date 2011-8-15 下午08:27:18
	 */

	public static String leftString(String str, String start, String end) {
		String left = str;
		int m, n;
		if (start != end && (m = str.indexOf(start)) != -1 && (n = str.indexOf(end)) != -1)
			left = str.substring(0, m) + str.substring(n + end.length());
		else if (start == end && (m = str.indexOf(start)) != -1 && (n = str.indexOf(end, m + start.length())) != -1)
			left = str.substring(0, m) + str.substring(n + end.length());
		return left;
	}

	public static String getHost(String str) {
		try {
			URI uri = new URI(str);
			return uri.getHost();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getFullHost(String str) {
		try {
			URI uri = new URI(str);
			return "http://" + uri.getHost() + "/";
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getHostReg(String str) {
		str = str.replaceAll("\\.", "\\\\.");
		return str;
	}

	public static String getPathReg(String str, String notReplace) {
		str = str.replaceAll(notReplace, "####");
		str = str.replaceAll("\\.", "\\\\\\\\.");
		str = str.replaceAll("[0-9]+", "[0-9]+");
		str = str.replaceAll("####", notReplace);
		return str;
	}

	public static void println(String str) {
		System.out.println(str);
	}

	public static int whichMatch(String str, String[] regs) {
		PatternCompiler compiler = new Perl5Compiler();
		// PatternMatcher matcher = new Perl5Matcher();
		for (int i = 0; i < regs.length; i++) {
			/*
			 * try { //Pattern pattern = compiler.compile(regs[i]); if
			 * (matcher.matches(str, pattern)) return i; } catch
			 * (MalformedPatternException e) { e.printStackTrace(); }
			 */
			/*
			 * java.util.regex.Pattern p = java.util.regex.Pattern.compile(regs[i]);
			 * Matcher m = p.matcher(str); if( m.matches() ) return i;
			 */
			if (str.matches(regs[i]))
				return i;

		}
		return -1;
	}

	/**
	 * @Title: getUrlPath
	 * @Description: URI 的PATH
	 * @param @param url
	 * @param @return
	 * @return String
	 * @throws
	 * @date 2011-8-15 下午08:26:01
	 */

	public static String getUrlPath(String url) {
		try {
			URI uri = new URI(url);
			return uri.getPath();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int findFirstReg(String str, String reg) {
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(reg);
		Matcher m = p.matcher(str);
		if (m.find())
			return m.start();
		return -1;
	}

	public static String getMatch(String str, String reg, int i) {
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(reg);
		Matcher m = p.matcher(str);
		String match = null;
		if (m.find()) {
			match = m.group(i);
		}
		if (match == null || match.length() == 0)
			return null;
		return match;
	}

	/**
	 * @Title: containStr
	 * @Description: 字符数组是否含有字符
	 * @param @param array
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 * @date 2011-8-15 下午08:24:22
	 */

	public static boolean containStr(String[] array, String str) {
		for (int i = 0; i < array.length; i++)
			if (str == array[i] || str.equals(array[i]))
				return true;
		return false;
	}

	/**
	 * @Title: getUrlEncode
	 * @Description: URL 编码
	 * @param @param str
	 * @param @return
	 * @return String
	 * @throws
	 * @date 2011-8-15 下午08:24:00
	 */

	public static String getBDUrlEncode(String str) {
		try {
			String url = URLEncoder.encode(str, "GBK");
			// System.out.println(url);
			return url;
		} catch (UnsupportedEncodingException e) {
			System.err.println("Url 解析失败    " + str);
			return null;
		}
	}

	public static String getBDUrlEncode(String str, String encode) {
		try {
			String url = URLEncoder.encode(str, encode);
			// System.out.println(url);
			return url;
		} catch (UnsupportedEncodingException e) {
			System.err.println("Url 解析失败    " + str);
			return null;
		}
	}

	/**
	 * @Title: subString2
	 * @Description:第一个start 以及之后的第一个end 之间 的字符串
	 * @param @param str
	 * @param @param start
	 * @param @param end
	 * @param @return
	 * @return String
	 * @throws
	 * @date 2011-8-15 下午08:28:54
	 */

	public static String subStringCon(String str, String start, String end) {
		String sub = null;
		int m, n;
		if ((m = str.indexOf(start)) != -1) {
			str = str.substring(m + 1);
			if ((n = str.indexOf(end)) != -1)
				sub = str.substring(0, n);
		}
		return sub;
	}

	public static List arrayToList(String[] str) {
		if (str == null)
			return null;
		List list = new ArrayList<String>();
		if (str.length == 0)
			return list;
		for (int i = 0; i < str.length; i++) {
			list.add(str[i]);
		}
		return list;
	}

	public static String[] listToArray(List list) {
		if (list == null || list.size() == 0)
			return null;
		String[] str = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			str[i] = (String) list.get(i);
		}
		return str;
	}

	public static String[] filtRepStr(String[] str) {
		if (str == null || str.length <= 1)
			return str;
		List list = arrayToList(str);
		if (list == null)
			return str;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i) == "") {
				list.remove(i);
				continue;
			}
			for (int j = 0; j < list.size(); j++) {
				if (j == i)
					continue;
				if (list.get(i).equals(list.get(j)) || list.get(i) == list.get(j)) {
					list.remove(i);
					break;
				}
			}
		}
		return listToArray(list);
	}

	public static String arrayToStr(String[] str, String split) {
		if (str == null || str.length == 0)
			return null;
		String string = "";
		for (int i = 0; i < str.length; i++) {
			string = string + str[i] + split;
		}
		return string;
	}

	public static String arrayToStr(boolean[] str, String split) {
		if (str == null || str.length == 0)
			return null;
		String string = "";
		for (int i = 0; i < str.length; i++) {
			string = string + str[i] + split;
		}
		return string;
	}

	public static String arrayToStr(double[] str, String split) {
		if (str == null || str.length == 0)
			return null;
		String string = "";
		for (int i = 0; i < str.length; i++) {
			string = string + str[i] + split;
		}
		return string;
	}
	public static List<String> strToList(String str, String split) {
		List list = new ArrayList<String>();
		if (str == null || split == null || str.length() == 0)
			return list;
		String[] strs = str.split(split);
		return arrayToList(strs);
	}

	public static int arrayContain(String[] array, String str) {
		if (array == null || array.length == 0 || str == null || str.length() == 0)
			return -1;
		for (int i = 0; i < array.length; i++) {
			if (array[i].equals(str) || array[i] == str)
				return i;
		}
		return -1;
	}

	public static String list2str(List<String> list) {
		if (list == null || list.size() == 0)
			return "";
		String string = "";
		for (int i = 0; i < list.size(); i++) {
			string = string + list.get(i);
		}
		return string;
	}

	public static String list2str(List<String> list, String split) {
		if (list == null || list.size() == 0)
			return "";
		String string = "";
		for (int i = 0; i < list.size(); i++) {
			string = string + list.get(i) + split;
		}
		if (string.length() > 0)
			string = string.substring(0, string.length() - split.length());
		return string;
	}

	public static int arraylistContain(List<String> list, String str) {
		if (list == null || list.size() == 0 || str == null || str.length() == 0)
			return -1;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).indexOf(str) != -1)
				return i;
		}
		return -1;
	}

	public static List<List<String>> str2arrayList(String str, String s1, String s2) {
		List<List<String>> list = new ArrayList<List<String>>();
		List<String> array = StringUtility.strToList(str, s1);
		if (array.isEmpty())
			return list;
		for (String splited : array) {
			List<String> temp = strToList(str, s2);
			if (!temp.isEmpty())
				list.add(temp);
		}
		return list;
	}

	public static String fillPreLength(String v, char c, int len) {
		if (v.length() >= len)
			return v.substring(0, len);
		return fillString(c, len - v.length()) + v;
	}

	public static String fillTailLength(String v, char c, int len) {
		if (v.length() >= len)
			return v.substring(0, len);
		return v + fillString(c, len - v.length());
	}

	public static String fillString(char c, int len) {
		char ch[] = new char[len];
		for (int i = 0; i < len; i++)
			ch[i] = c;
		String str = new String(ch);
		return str;

	}

	public static CharSequence chArray2Str(char[] ch, boolean addSplit, char split) {
		StringBuffer string = new StringBuffer();
		for (int i = 0; i < ch.length - 1; i++) {
			string.append(ch[i]);
			if (addSplit)
				string.append(split);
		}
		string.append(ch[ch.length - 1]);
		return string;
	}

	public static final String NOTCHARACTOR = "[^\\p{Punct}0-9A-Za-z\u4e00-\u9fa5]";

	public static void main(String[] args) {
		// System.out.println(StringUtility.leftString("wer,wner,ouoo", ",", ","));
		StringUtility.println(StringUtility.getHostReg("http://www.uu.dd"));
		StringUtility.println(StringUtility.getPathReg("http://detail.zol.com.cn/228/227845/review_0_2_0_1.shtml", "22"));
		StringUtility.println(StringUtility.getMatch("/141/140974/review.shtml", "([0-9]*)\\.", 1));
		StringUtility.println("" + StringUtility.findFirstReg("www.123.dd.88", "[0-9]+"));
		StringUtility
				.println(""
						+ whichMatch("http://detail.zol.com.cn/cell_phone_index/subcate57_288_list_1.html", new String[] { "",
								"http://detail\\.zol\\.com\\.cn/cell_phone_index/subcate57_[0-9]+_list_[0-9]+\\.html" }));

		StringUtility.println(StringUtility.subStringCon("ni们号码们也", "们", "们"));
		StringUtility.println((char) 12288 + "");
	}

}
